package com.cham.pinglun.mapper;

import com.cham.pinglun.dto.PinglunInfo;
import com.cham.pinglun.entity.Pinglun;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @author Howl-Xu
 * @version 1.0.0
 * @Description: 请描述一下这个类文件吧
 * @createTime 2020年03月31日 16:12:00
 */
public interface PinglunMapper {

    @Insert("insert into cham_pinglun(video_id,user_uuid,content,pinglun_create_time) VALUES (#{videoId},#{userUUid},#{content},#{pinglunCreateTime})")
    void savePinglun(Pinglun pinglun);

    @Select("select\n" +
            "\tc.*\n" +
            "from\n" +
            "\t(\n" +
            "\tselect\n" +
            "\t\tb.*\n" +
            "\tfrom\n" +
            "\t\t(\n" +
            "\t\tselect\n" +
            "\t\t\ta.*,\n" +
            "\t\t\t(@pinglunIndex := @pinglunIndex + 1) pinglunIndex\n" +
            "\t\tfrom\n" +
            "\t\t\t(\n" +
            "\t\t\tselect\n" +
            "\t\t\t\tcp.pinglun_id,\n" +
            "\t\t\t\tcp.content,\n" +
            "\t\t\t\tcp.user_uuid,\n" +
            "\t\t\t\tcp.pinglun_create_time,\n" +
            "\t\t\t\tsu.nick_name,\n" +
            "\t\t\t\tchi.img_base64\n" +
            "\t\t\tfrom\n" +
            "\t\t\t\tcham_pinglun cp,\n" +
            "\t\t\t\tsys_user su,\n" +
            "\t\t\t\tcham_head_img chi\n" +
            "\t\t\twhere\n" +
            "\t\t\t\tcp.video_id = #{videoId}\n" +
            "\t\t\t\tand cp.user_uuid = su.user_uuid\n" +
            "\t\t\t\tand chi.user_uuid = su.user_uuid\n" +
            "\t\t\torder by\n" +
            "\t\t\t\tcp.pinglun_id asc ) a,\n" +
            "\t\t\t(\n" +
            "\t\t\tselect\n" +
            "\t\t\t\t@pinglunIndex := 0) as pinglunIndex ) b\n" +
            "\tlimit #{start},\n" +
            "\t#{pageSize}) c\n" +
            "order by\n" +
            "\tc.pinglunIndex desc")
    List<PinglunInfo> getPinglunInfo(@Param("videoId") int videoId, @Param("start") int start, @Param("pageSize") int pageSize);

    @Select("select\n" +
            "\tcount(1)\n" +
            "from\n" +
            "\tcham_pinglun cp\n" +
            "where\n" +
            "\tcp.video_id = #{videoId}")
    int getPinglunInfoCount(int videoId);
}
